import { ref, computed } from "vue";
import { useRoute, useRouter } from "vue-router";
import { FormTypeEnum } from "@/utils/enum";
import {
  noContactReasonFormat,
  sourceFormat,
  varityFormResult,
  factFindingFormat,
  onsiteFormat,
  replyMethod,
  booleanFormat,
  formatAppeal,
  findingOfAuditFormat,
  formatDelayPeriod,
  applyReasonFormat,
  priorityFormat,
  levelFormat,
  priorityFormat2,
  isStatisFied,
  isSolveFormat,
  isContactFormat
} from "@/utils/format";
import { parseTime } from "@/utils/utils";
import { showToast } from "vant";
import api from "@/api/index";
import { globalEvent } from "@/main";
const useFormDetail = (formParam) => {
  /*
   * 页面类型  待办 todo 延期 delay 退单 rollback
   */
  const formType = useRoute().query.type;
  const identifer = useRoute().query.id;
  // const baseURL = process.env.VUE_APP_URL;
  const baseURL = 'https://12345.shsports.cn';
  //填写表单内容
  const formParams = ref(formParam);
  const router = useRouter();
  //申请信息标题展示
  const appleInfoTitle = computed(() => {
    let title = "";
    switch (formType) {
      case FormTypeEnum.TODO:
        title = "代办工单";
        break;
      case FormTypeEnum.DELAY:
        title = "延期工单";
        break;
      case FormTypeEnum.ROLLBACK:
        title = "退工工单";
        break;
    }

    return title + "申请信息"
  });
  /*
   * 查看工单内容
   */
  const watchFormInfoList = computed(() => {
    const value = formParams.value;
    switch (formType) {
      case FormTypeEnum.TODO:
        return list.value;
      default:
        return [
          { title: "工单编号", content: value.workOrderNumber },
          { title: "联系人",  content: value.parameter1 },
          { title: "联系电话", content: value.parameter2 },
          { title: "来电类别", content: value.wpType },
          { title: "联系地址", content: value.parameter3 },
          { title: "提交日期", content: parseTime(value.submitTime,"{y}-{m}-{d}") },
          { title: "12345截止日期(15个工作日)", content: parseTime(value.processingTimeLimit,"{y}-{m}-{d}") },
          { title: "工单来源", content: sourceFormat(value.source) },
          { title: "工单优先级", content: levelFormat(value.level) },
          { title: "是否催单", content: priorityFormat(value.priority) },
          { title: "成立时间", content: parseTime(value.createTime,"{y}-{m}-{d}") },
          { title: "是否核单", content: priorityFormat2(value.priority) },
          { title: "一级分类", content: value.contentCategory },
          { title: "二级分类", content: value.contentParentCategory },
          { title: "转交日期", content: value.transmitTime },
          { title: "转交处理截止日期(14个工作日)", content: value.transmitEndTime },
          { title: "先行联系", content: isContactFormat(value.isContact) },
          {
            title: "未联原因",
            content: noContactReasonFormat(value.noContactReason),
          },
          { title: "12345工单内容", content: value.parameter4 },
        ];
    }
  });

  //查看审核列表数据
  const watchReviewList = computed(() => {
    return [
      {
        title: "审核结果",
        content: findingOfAuditFormat(formParams.value.findingOfAudit),
      },
      {
        title: "审核意见",
        content: formParams.value.confirmOpinion,
      },
    ];
  });

  //查看申请信息
  const watchAppleList = computed(() => {
    const value = formParams.value;
    switch (formType) {
      case FormTypeEnum.DELAY:
        return [
          { title: "申请原因", content: applyReasonFormat(value.applyReason) },
          { title: "延期期限", content: formatDelayPeriod(value.delayPeriod) },
          { title: "延期到期日", content: value.delayTime },
          { title: "联系电话", content: value.phone },
          { title: "经办人", content: value.operator },
          { title: "负责人", content: value.head },
          { title: "已用件数", content: value.num },
          { title: "初始配额", content: value.quota },
          { title: "备注", content: value.notes },
          { title: "延期申请通知书", content: value.notice },
        ];

      case FormTypeEnum.ROLLBACK:
        return [
          { title: "申请原因", content: applyReasonFormat(value.applyReason) },
          { title: "退单人员", content: value.returnPerson },
          { title: "退单理由", content: value.returnReason },
          { title: "退单说明", content: value.returnDescription },
        ];

      case FormTypeEnum.TODO:
        return switchManageList.value;

      default:
        return [];
    }
  });

  //审核状态列表数据
  const reviewList = computed(() => {
    return [
      {
        title: "审核结果",
        content: formParams.value.findingOfAudit,
        formart: findingOfAuditFormat(formParams.value.findingOfAudit),
        type: "options",
        isRequired: true,
        maxlength: 0,
      },
      {
        title: "审核意见",
        content: formParams.value.confirmOpinion,
        type: "input",
        isRequired: true,
        maxlength: 150,
      },
    ];
  });

  /****
   * 
   * ************************************* 上面是延期，退单状态的审核和查看显示数据源 *******************
   * /

  /**
   * 查看添加一种状态 代办工单也有查看状态
   * 
  */
  const list = computed(() => {
    const value = formParams.value;
    return [
      { title: "工单编号", content: value.workOrderNumber },
      { title: "联系人", content: value.parameter1 },
      { title: "联系电话", content: value.parameter2 },
      { title: "来电类别", content: value.wpType },
      { title: "联系地址", content: value.parameter3 },
      { title: "提交日期", content: parseTime(value.submitTime,"{y}-{m}-{d}") },
      { title: "12345截止日期(15个工作日)", content: parseTime(value.processingTimeLimit,"{y}-{m}-{d}")  },
      { title: "工单来源", content: sourceFormat(value.source) },
      { title: "工单优先级", content: levelFormat(value.level)},
      { title: "是否催单", content: priorityFormat(value.priority) },
      { title: "成立时间", content: parseTime(value.createTime) },
      { title: "是否核单", content: priorityFormat2(value.priority) },
      { title: "一级分类", content: value.contentCategory },
      { title: "二级分类", content: value.contentParentCategory },
      { title: "转交日期", content: value.transmitTime },
      { title: "转交处理截止日期(14个工作日)", content: value.transmitEndTime },
      {
        title: "未联原因",
        content: noContactReasonFormat(value.noContactReason),
      },
      { title: "12345工单内容", content: value.parameter4 },
      { title: "是否抽查", content: value.isChecks },
      { title: "抽查结果", content: value.checkResult },
    ];
  });

  //办结报告
  const todoReportList = computed(() => {
    const value = formParams.value;
    return [
      {
        title: "事实认定",
        content: factFindingFormat(value.factFinding),
      },
      {
        title: "认定说明",
        content: value.factDescription,
      },
      {
        title: "现场查看",
        content: onsiteFormat(value.onsite),
      },
      {
        title: "诉求认定",
        content: formatAppeal(value.appeal),
      },
      {
        title: "认定说明",
        content: value.appealDescription,
      },
      {
        title: "答复方式",
        content: replyMethod(value.replyMethod),
      },
      {
        title: "答复市民时间",
        content: parseTime(value.replyTime,"{y}-{m}-{d}"),
      },
      {
        title: "办结日期",
        content: parseTime(value.completionTime,"{y}-{m}-{d}"),
      },
      {
        title: "是否解决",
        content: isSolveFormat(value.isSolve),
      },
      {
        title: "是否满意",
        content: isStatisFied(value.isSatisfied),
      },
      {
        title: "办结汇报",
        content: value.completionReport,
      },
      {
        title: "答复市民要点",
        content: value.replyHighlight,
      },
      {
        title: "市民反馈说明",
        content: value.feedbackExplanation,
      },

      //下面三项只有只有私教工单才显示，其他类型工单不显示
      {
        title: "是否拖延（3个月以上）",
        content: booleanFormat(value.isDelay),
      },
      {
        title: "是否拒不配合",
        content: booleanFormat(value.isCoordinate),
      },
      {
        title: "是否关门跑路",
        content: booleanFormat(value.isRunAway),
      },
    ];
  });

  const delayReportList = computed(() => {
    const value = formParams.value;
    return [
      {
        title: "申请原因",
        content: applyReasonFormat(value.applyReason),
      },
      {
        title: "延期期限",
        content: formatDelayPeriod(value.delayPeriod),
      },
      {
        title: "延期到期日",
        content: parseTime(value.delayTime,"{y}-{m}-{d}"),
      },
      {
        title: "联系电话",
        content: value.phone,
      },
      {
        title: "经办人",
        content: value.operator,
      },
      {
        title: "负责人",
        content: value.head,
      },
      {
        title: "已用件数",
        content: value.num,
      },
      {
        title: "初始配额",
        content: value.quota,
      },
      {
        title: "备注",
        content: value.notes,
      },
      {
        title: "延期申请通知书",
        content: value.notice,
      },
    ];
  });

  //退单申请信息
  const rollbackReportList = computed(() => {
    return [
      {
        title: "退单理由",
        content: findingOfAuditFormat(formParams.value.returnReason),
      },
      {
        title: "退单说明",
        content: formParams.value.notice,
      },
    ];
  });

  //根据状态切换展示
  const switchManageList = computed(() => {
    let type = formParams.value.status;
    if (type == "9") {
      return todoReportList.value;
    } else if (type == "11") {
      return delayReportList.value;
    } else if (type == "13") {
      return rollbackReportList.value;
    } else {
      return todoReportList.value;
    }
  });
  /**
   * ****************************** 上面是代办工单展示内容
   *
   */

  //提交表单内容
  const submitForm = async () => {
    if (!varityFormResult([reviewList.value])) {
      return;
    }

    formParams.value.orderInfoId = identifer;
    // const { data } = await api.institutionSubmit(params);
    // console.log("表单暂存结果：", data);
    api.institutionMainExamine(formType, formParams.value).then(() => {
      showToast("提交成功");
      formParams.value = {};
      formParams.value.formType = formType
      globalEvent.emit("myGlobalEvent", formParams.value);
      router.back();
    });
  };
  return {
    baseURL,
    formType,
    formParams,
    identifer,
    appleInfoTitle,
    watchFormInfoList,
    watchAppleList,
    watchReviewList,
    reviewList,
    list,
    todoReportList,
    delayReportList,
    rollbackReportList,
    switchManageList,
    submitForm,
  };
};

export { useFormDetail };
